<?php

namespace App\Plugin\Website\Models;

use App\Models\BaseModel;
use App\Plugin\HostServer\Models\HostServerBase;
use Illuminate\Database\Eloquent\Builder;

class PnHostSvNav extends HostServerBase
{
    //
    public static function category($group_type){
        $data=self::where('group_type',$group_type)->get()->toArray();
        $data=get_tree_option($data,0);
        return $data;
    }

    /**
     * 写入缓存
     * @return mixed
     * @throws \Exception
     */
    public static function updateCache($type=''){
        $nav=self::checked()->sortDesc()->get()->toArray();
        $data=[];
        if(!empty($nav))
        {
            if($type){
                foreach ($nav as $k=>$v)
                {
                    if($v['group_type']==$type)
                    {
                        $data[]=$v;
                    }
                }
            }else
            {
                $data=$nav;
                $nav_arr=[];
                $foot_arr=[];
                foreach ($nav as $k=>$v)
                {
                    if($v['group_type']=='nav')
                    {
                        $nav_arr[]=$v;
                    }
                    if($v['group_type']=='footer')
                    {
                        $foot_arr[]=$v;
                    }
                }
                cache()->forever('host_server_nav_nav',$nav_arr);
                cache()->forever('host_server_nav_footer',$foot_arr);
            }
        }
        cache()->forever('host_server_nav'.($type?'_'.$type:''),$data);
        return $data;
    }
}
